package com.woniu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Account;
import com.woniu.mapper.AccountMapper;
import com.woniu.service.AccountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper mapper;

    @Override
    public List<Account> findAllAccounts() {
        return mapper.selectAll();
    }

    @Override
//    @Cacheable(value = "account", key = "#root.methodName+#root.args[0]+#root.args[1]")
    public PageInfo<Account> findAccountByPage(int pageNum, int pageSize) {
        Page<Account> page = PageHelper.startPage(pageNum, pageSize);
        mapper.selectAll();
        return page.toPageInfo();
    }

    @Override
    public Account findAccountByLogin(String accountName) {
        Example example = new Example(Account.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("accountName", accountName);
        return mapper.selectOneByExample(example);
    }

    @Override
    public Account findAccountById(int id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean isAccountNameExists(String accountName) {
        Example example = new Example(Account.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("accountName", accountName);
        return mapper.selectCountByExample(example) > 0;
    }

    @Override
    public boolean registerAccount(Account account) {
        int insertCount = mapper.insert(account);
        return insertCount > 0;
    }

    @Override
    public boolean updateAccountByAccountId(Account account) {
        int i = mapper.updateByPrimaryKeySelective(account);
        return i > 0;
    }

    @Override
    public boolean updatePasswordByAccountId(Account account) {
        int i = mapper.updateByPrimaryKeySelective(account);
        return i > 0;
    }


}
